package com.litan.chapterfive;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;

public class MyView extends View {

    private Season mSeason;
    public MyView(Context context){
        this(context, null);
    }

    public MyView(Context context, AttributeSet attrs){
        this(context, attrs, 0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measuredHeight = measureHeight(heightMeasureSpec);
        int measuredWidth = measureWidth(widthMeasureSpec);

        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    private int measureHeight(int measureSpec){
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        // 计算该View的高
        // 若无指定限制(specMode)，则使用默认大小（单位:像素）
        int result = 500;

        if (specMode == MeasureSpec.AT_MOST){
            // 在此最大尺寸范围内计算控件的理想尺寸
            // 若控件填充了可用空间，则返回外部边界值
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY){
            // 若控件能符合这些界限，则返回该值
            result = specSize;
        }
        return result;
    }

    private int measureWidth(int measureSpec){
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        // 若无指定限制(specMode)，则使用默认大小（单位:像素）
        int result = 500;

        if (specMode == MeasureSpec.AT_MOST){
            // 在此最大尺寸范围内计算控件的理想尺寸
            // 若控件填充了可用空间，则返回外部边界值
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY){
            // 若控件能符合这些界限，则返回该值
            result = specSize;
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //自定义可视化页面

        // 根据上一次调用的onMeasure()获取控件的大小
        int height = getMeasuredHeight();
        int width = getMeasuredWidth();

        // 找到中心点
        int px = width / 2;
        int py = height / 2;

        //创建新的画笔
        //为提高效率，应在View中的构造函数中完成
        Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//字体样式
        mTextPaint.setColor(Color.WHITE);//字体颜色

        // 定义要绘制的自定义字符串
        String displayText = "Hello View!";
        // 测量字符串的宽度
        float textWidth = mTextPaint.measureText(displayText);
        // 在控件中心绘制字符串
        canvas.drawText(displayText,px-textWidth/2, py, mTextPaint);

    }

    // 与用户的交互

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    // 广播无障碍事件
    public void setSeason(Season season){
        mSeason = season;
        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
    }



    class Season{

    }

}
